package io.renren.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.annotation.LoginUser;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.dto.CollarDTO;
import io.renren.dto.StockOrderDTO;
import io.renren.dto.TemporaryOrderDTO;
import io.renren.entity.UserEntity;
import io.renren.service.CollarService;
import io.renren.service.SysDeptService;
import io.renren.service.TemporaryOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 辅材领用表
 *
 * @author 罗胜平 781757616@qq.com
 * @since 1.0.0 2019-11-22
 */
@RestController
@RequestMapping("api/collar")
@Api(tags="辅材领用")
public class CollarController {
    @Autowired
    private CollarService collarService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private TemporaryOrderService temporaryOrderService;

    @GetMapping("page")
    @ApiOperation("辅材领用订单列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Map> page(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        PageData<CollarDTO> page = collarService.getOrder(params,entity);
        HashMap<String, Object> map = new HashMap<>();
        TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(3, entity.getId(), entity.getDeptId());
        ArrayList<CollarDTO> arraylist = new ArrayList<>();
        Page page1 = (Page)params.get("page");
        if(1 == page1.getCurrent() && null != temOrder){
            CollarDTO collarDTO = new CollarDTO();
            collarDTO.setCreateDate(temOrder.getCreateTime());
            collarDTO.setFlag(1);
            String deptName = sysDeptService.getDeptName(entity.getDeptId()).getName()+"主食申领清单草稿";
            collarDTO.setChedRemake(deptName);
            arraylist.add(collarDTO);
        }
        for (CollarDTO collarDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(collarDTO.getCanteenId()).getName()+"主食申领清单";
            collarDTO.setChedRemake(deptName);
            arraylist.add(collarDTO);
        }
        page.setList(arraylist);
        map.put("list",page);
        // 返回待处理的订单的数量 TODO: 2019/12/12
        CollarDTO total = collarService.getTotal(entity.getId(), null, entity.getDeptId(), 4);
        map.put("list",page);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<CollarDTO> get(@PathVariable("id") Long id){
        CollarDTO data = collarService.get(id);

        return new Result<CollarDTO>().ok(data);
    }
    /**
     * 辅材申请订单
     * @return
     */
    @PostMapping("saveCollarOrder")
    @ApiOperation("辅材申请订单")
    public Result saveCollarOrder(@RequestBody JSONObject params,@LoginUser UserEntity entity){
        collarService.saveCollarOrder(params,entity);
        return new Result<>();
    }

    @GetMapping("pagesTorehouseClerk")
    @ApiOperation("仓储员分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Map> pagesTorehouseClerk(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity) {
        PageData<CollarDTO> page = collarService.pagesTorehouseClerk(params,entity);
        HashMap<String, Object> map = new HashMap<>();
        for (CollarDTO collarDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(collarDTO.getCanteenId()).getName()+"主食申领清单";
            collarDTO.setChedRemake(deptName);
        }
        // 返回待处理的订单的数量 TODO: 2019/12/12
        CollarDTO total = collarService.getTotal(null, null, entity.getDeptId(), 3);
        map.put("list",page);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }
    @GetMapping("pageAdmin")
    @ApiOperation("食堂管理员分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<Map> pageAdmin(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        PageData<CollarDTO> page = collarService.pageAdmin(params);
        HashMap<String, Object> map = new HashMap<>();
        for (CollarDTO collarDTO:page.getList()) {
            String deptName = sysDeptService.getDeptName(collarDTO.getCanteenId()).getName()+"主食申领清单";
            collarDTO.setChedRemake(deptName);
        }
        // 返回待处理的订单的数量 TODO: 2019/12/12
        CollarDTO total = collarService.getTotal(null, null,null, 1);
        map.put("list",page);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }
    @PostMapping("pageAdminUpdate")
    @ApiOperation("食堂管理员审核")
    public Result updateExam(@RequestBody CollarDTO dto, @LoginUser UserEntity entity){
        return collarService.updateExamine(dto,entity);
    }
    @GetMapping("home")
    @ApiOperation("辅材申领列表")
    public Result<Map> home(@ApiIgnore @RequestParam Map<String, Object> params, @LoginUser UserEntity entity){
        List<CollarDTO> page = collarService.home(params,entity);
        HashMap<String, Object> map = new HashMap<>();
        TemporaryOrderDTO temOrder = temporaryOrderService.getTemOrder(3, entity.getId(), entity.getDeptId());
        if(null == params.get("page") && null != temOrder){
            CollarDTO collarDTO = new CollarDTO();
            collarDTO.setCreateDate(temOrder.getCreateTime());
            collarDTO.setFlag(1);
            collarDTO.setCanteenId(entity.getDeptId());
            String deptName = sysDeptService.getDeptName(entity.getDeptId()).getName()+"主食申领清单草稿";
            collarDTO.setChedRemake(deptName);
            page.add(collarDTO);
        }
        for (CollarDTO collarDTO:page) {
            String deptName = sysDeptService.getDeptName(collarDTO.getCanteenId()).getName()+"主食申领清单";
            if(1!=collarDTO.getFlag()){
                collarDTO.setChedRemake(deptName);
            }
        }

        CollarDTO total = collarService.getTotal(null, null, entity.getDeptId(), 4);
        map.put("list",page);
        map.put("pending",total.getPending());
        return  new Result<Map>().ok(map);
    }
}